#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <UTF.h>
#include "RegExpUtil.h"
#include "StringUtil.h"

#define KLogFileName "c:\\data\\TaobaoMobile\\log.txt"


HBufC8* CRegExpUtil::getRegExpResult(const TDesC8& aRegexp, const TDesC8& aData)
	{
	char* Regexp = CStringUtil::TransformTDes8ToChar(aRegexp);
	char* Data = CStringUtil::TransformTDes8ToChar(aData);
	HBufC8* result = getRegExpResult(Regexp,Data);
	delete []Regexp;
	delete []Data;
	return result;
	}
	
HBufC*  CRegExpUtil::getGbkRegExpResult(const TDesC8& aRegexp, const TDesC8& aData)
	{
	HBufC8* value = CRegExpUtil::getRegExpResult(aRegexp, aData);
	HBufC* result = NULL;
	if (value)
		result = CStringUtil::Gbk2UniL(*value);
	delete value;
	value = NULL;
	return result;
	}
		
HBufC*  CRegExpUtil::getUnicodeRegExpResult(const TDesC8& aRegexp, const TDesC8& aData)
	{
	HBufC8* value = CRegExpUtil::getRegExpResult(aRegexp, aData);
	HBufC* result = NULL;
	if (value)
		result = CStringUtil::TransformHBufC8ToHBufC(value);
	delete value;
	value = NULL;
	return result;
	}

HBufC8* CRegExpUtil::getRegExpResult(const TDesC& aRegexp, const TDesC& aData)
	{
	const char* Regexp = CStringUtil::TransformTDesToConstChar(aRegexp);
	const char* Data = CStringUtil::TransformTDesToConstChar(aData);
	HBufC8* result = getRegExpResult(Regexp,Data);
	delete []Regexp;
	delete []Data;
	return result;
	}

HBufC8* CRegExpUtil::getRegExpResult(const char* aRegexp, const char* aData)
{
    int groupStart, groupEnd;
    const char *resultStringTemp2;
    HBufC8 *resultHbuf;
    CRegexpT<char> prodRegexpT;
    prodRegexpT.Compile(aRegexp);

    resultHbuf = NULL;
    MatchResult result = prodRegexpT.Match(aData);
    if (result.IsMatched() && result.MaxGroupNumber() >= 0)
    {
		int index = 0;
		if (result.MaxGroupNumber() >= 1)
			index = 1;
        groupStart = result.GetGroupStart(index);
        groupEnd = result.GetGroupEnd(index);
        resultStringTemp2 = aData;
        resultStringTemp2 += groupStart;
        TPtr8 tFileName8((unsigned char*) resultStringTemp2, groupEnd
                - groupStart, groupEnd - groupStart);
        resultHbuf = tFileName8.AllocL();
    }
    return resultHbuf;
}

HBufC* CRegExpUtil::getGbkRegExpResult(const char* aRegexp, const char* aData)
	{
	HBufC8* value = CRegExpUtil::getRegExpResult(aRegexp, aData);
	HBufC* result = NULL;
	if (value)
		result = CStringUtil::Gbk2UniL(*value);
	delete value;
	value = NULL;
	return result;
	}

HBufC* CRegExpUtil::getUnicodeRegExpResult(const char* aRegexp, const char* aData)
	{
	HBufC8* value = CRegExpUtil::getRegExpResult(aRegexp, aData);
	HBufC* result = NULL;
	if (value)
		result = CStringUtil::TransformHBufC8ToHBufC(value);
	delete value;
	value = NULL;
	return result;
	}

CDesC8Array* CRegExpUtil::getRegExpResults(const TDesC8& aRegexp, const TDesC8& aData)
	{
	char* Regexp = CStringUtil::TransformTDes8ToChar(aRegexp);
	char* Data = CStringUtil::TransformTDes8ToChar(aData);
	CDesC8Array* result = getRegExpResults(Regexp,Data);
	delete []Regexp;
	delete []Data;
	return result;
	}

CDesC8Array* CRegExpUtil::getRegExpResults(const TDesC& aRegexp, const TDesC& aData)
	{
	const char* Regexp = CStringUtil::TransformTDesToConstChar(aRegexp);
	const char* Data = CStringUtil::TransformTDesToConstChar(aData);
	CDesC8Array* result = getRegExpResults(Regexp,Data);
	delete []Regexp;
	delete []Data;
	return result;
	}
		
CDesC8Array* CRegExpUtil::getRegExpResults(const char* aRegexp, const char* aData)
	{
	CDesC8Array* Array= new (ELeave) CDesC8ArrayFlat(5);
	const char *resultStringTemp2;
    HBufC8 *resultHbuf;
    CRegexpT<char> prodRegexpT;
    prodRegexpT.Compile(aRegexp);
    // prepare
    CContext * pContext = prodRegexpT.PrepareMatch(aData);
    // loop
    MatchResult result = prodRegexpT.Match(pContext);
    while( result.IsMatched() )
    {        
        resultStringTemp2 = aData;
        resultStringTemp2 += result.GetStart();
        TPtr8 tFileName8((unsigned char*) resultStringTemp2, result.GetEnd() - result.GetStart(), result.GetEnd() - result.GetStart());
        resultHbuf = tFileName8.AllocL();
        Array->AppendL(*resultHbuf);
        delete resultHbuf;
        resultHbuf = NULL;
        // get next
        result = prodRegexpT.Match(pContext);
    }
    // release
    prodRegexpT.ReleaseContext(pContext);
	return Array;
	}

CDesC8Array* CRegExpUtil::getGroupRegExpResult(const TDesC8& aRegexp, const TDesC8& aData)
	{
	char* Regexp = CStringUtil::TransformTDes8ToChar(aRegexp);
	char* Data = CStringUtil::TransformTDes8ToChar(aData);
	CDesC8Array* result = getGroupRegExpResult(Regexp,Data);
	delete []Regexp;
	delete []Data;
	return result;
	}

CDesC8Array* CRegExpUtil::getGroupRegExpResult(const TDesC& aRegexp, const TDesC& aData)
	{
	const char* Regexp = CStringUtil::TransformTDesToConstChar(aRegexp);
	const char* Data = CStringUtil::TransformTDesToConstChar(aData);
	CDesC8Array* result = getGroupRegExpResult(Regexp,Data);
	delete []Regexp;
	delete []Data;
	return result;
	}
		
CDesC8Array* CRegExpUtil::getGroupRegExpResult(const char* aRegexp, const char* aData)
	{
	CDesC8Array* Array= new (ELeave) CDesC8ArrayFlat(5);
    CRegexpT<char> regexpT;
    regexpT.Compile(aRegexp);
    CContext *context;
    context = regexpT.PrepareMatch(aData);
    MatchResult result = regexpT.Match(context);
    HBufC8* groupValue = NULL;
    if (result.IsMatched())
    	{
		for(int i = 0; i <= result.MaxGroupNumber(); i++)
			{
			groupValue = CRegExpUtil::getResultFromGroupByIndex(result, i, aData);
			if (groupValue)
				Array->AppendL(*groupValue);
			else
				Array->AppendL(KNullDesC8);
			delete groupValue;
			groupValue = NULL;
			}
    	}
    regexpT.ReleaseContext(context);
    return Array;
	}

HBufC8* CRegExpUtil::getResultFromGroupByIndex(MatchResult &result, int index, const char *data)
	{
    int groupStart, groupEnd;
    HBufC8 *resultData = NULL;
    char *htmlData;
    const char *tempHtml;
    groupStart = result.GetGroupStart(index);
    groupEnd = result.GetGroupEnd(index);
    htmlData = (char*) malloc(groupEnd - groupStart);
    tempHtml = data;
    tempHtml += groupStart;
    memcpy(htmlData, tempHtml, groupEnd - groupStart);
    TPtr8 tFileName8((unsigned char*) htmlData, groupEnd - groupStart, groupEnd - groupStart);
    resultData = tFileName8.AllocL();
    free(htmlData);
    return resultData;
	}
